package com.yanqu.road.entity.activity.dunhuangshiku;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.DataStatus;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 敦煌石窟区服玩家的信息
 * @Author cwq
 * @Data 2021/7/20 11:18
 */
@FieldTypeAnnotation(tableName = "t_u_activity_dunhuangshiku_user_data")
public class DunHuangShiKuServerUserData extends DataObject {

    @FieldTypeAnnotation(select = true, update = true)
    private long activityId;
    @FieldTypeAnnotation(select = true, update = true)
    private long userId;
    @FieldTypeAnnotation(desc = "玩家触发商品的时间 k:商品id v:最近的触发时间")
    private Map<Integer,Integer> touchMallMap = new ConcurrentHashMap<>();

    private Map<Integer, Long> boxKeyNumMap = new ConcurrentHashMap<>();
    //触发礼包次数
    private Map<Integer,Integer> touchMallTimesMap = new ConcurrentHashMap<>();

    public void setActivityId(long activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public void setUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            setOp(DataStatus.Update);
        }
    }

    public void setTouchMallMap(Map<Integer, Integer> touchMallMap) {
        this.touchMallMap = touchMallMap;
        setUpdateOption();
    }

    public long getActivityId() {
        return activityId;
    }

    public long getUserId() {
        return userId;
    }

    public Map<Integer, Integer> getTouchMallMap() {
        return touchMallMap;
    }

    public String getTouchMallMapJsonStr() {
        return JSON.toJSONString(touchMallMap);
    }

    public int getTouchMallTime(int mallId){
        if(touchMallMap.containsKey(mallId)){
            return touchMallMap.get(mallId);
        }
        return 0;
    }

    public void setTouchMallTime(int mallId, int time){
        touchMallMap.put(mallId, time);
        setUpdateOption();
    }

    public void removeTouchMall(int mallId){
        touchMallMap.remove(mallId);
        setUpdateOption();
    }

    public Map<Integer, Long> getBoxKeyNumMap() {
        return boxKeyNumMap;
    }

    public void setBoxKeyNumMap(Map<Integer, Long> boxKeyNumMap) {
        this.boxKeyNumMap = boxKeyNumMap;
        setUpdateOption();
    }

    public void addBoxKeyNum(int goodsId, long num) {
        if (boxKeyNumMap.containsKey(goodsId)) {
            boxKeyNumMap.put(goodsId, boxKeyNumMap.get(goodsId) + num);
        }else {
            boxKeyNumMap.put(goodsId, num);
        }
        setUpdateOption();
    }

    public Map<Integer, Integer> getTouchMallTimesMap() {
        return touchMallTimesMap;
    }

    public String getTouchMallTimesMapJsonStr() {
        return JSON.toJSONString(touchMallTimesMap);
    }

    public void setTouchMallTimesMap(Map<Integer, Integer> touchMallTimesMap) {
        this.touchMallTimesMap = touchMallTimesMap;
        setUpdateOption();
    }

    public int getTouchMallTimes(int mallId){
        if(touchMallTimesMap.containsKey(mallId)){
            return touchMallTimesMap.get(mallId);
        }
        return 0;
    }

    public void addTouchMallTimes(int mallId, int times){
        int totalTimes = getTouchMallTimes(mallId);
        totalTimes += times;
        touchMallTimesMap.put(mallId, totalTimes);
        setUpdateOption();
    }

    public void clearTouchMallTimes(int mallId){
        touchMallTimesMap.remove(mallId);
        setUpdateOption();
    }

    public void reduceTouchMallTimes(int mallId, int times){
        int totalTimes = getTouchMallTimes(mallId);
        totalTimes -= times;
        if(totalTimes < 0){
            totalTimes = 0;
        }
        touchMallTimesMap.put(mallId, totalTimes);
        setUpdateOption();
    }
}
